package com.weimei.conversionTreasure.viewModel;

import android.app.Activity;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.lxj.xpopup.XPopup;
import com.weimei.conversionTreasure.http.Repository;
import com.weimei.conversionTreasure.http.entity.BaseResult;
import com.weimei.conversionTreasure.utils.UserUtils;
import com.weimei.conversionTreasure.utils.ValidateUtils;
import com.weimei.conversionTreasure.weight.BottomRulePop;
import com.weimei.conversionTreasure.weight.BottomWheelPop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class AddBudgetVM extends BaseViewModel<Repository> {
    public AddBudgetVM(@NonNull Application application, Repository model) {
        super(application, model);
    }

    Activity mActivity;
    @Override
    public void setActivity(Activity activity) {
        super.setActivity(activity);
        mActivity = activity;
    }

    public ObservableField<String> types = new ObservableField<>("月");
    public ObservableField<String> category = new ObservableField<>("支出");
    public ObservableField<String> price = new ObservableField<>();

    public BindingCommand back = new BindingCommand(() -> finish());

    public BindingCommand save = new BindingCommand(() -> {
        if (!ValidateUtils.isFastDoubleClick()){
            save();
        }
    });


    public BindingCommand type = new BindingCommand(() ->{
        List<String> list = new ArrayList<>();
        list.add("月");
        list.add("年");
        new XPopup.Builder(mActivity)
                .moveUpToKeyboard(false)//如果不加这个，评论弹窗会移动到软键盘上面
                .enableDrag(true)
                .asCustom(new BottomWheelPop(mActivity, list, s1 -> types.set(s1)))
                .show();
    });

    public BindingCommand classType = new BindingCommand(() -> {
        List<String> list = new ArrayList<>();
        list.add("支出");
        list.add("收入");
        new XPopup.Builder(mActivity)
                .moveUpToKeyboard(false)//如果不加这个，评论弹窗会移动到软键盘上面
                .enableDrag(true)
                .asCustom(new BottomWheelPop(mActivity, list, s1 -> category.set(s1)))
                .show();
    });


    private void save() {

        if (ValidateUtils.isEmptyString(price.get())){
            ToastUtils.showShort("请输入预算金额");
            return;
        }

        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        map.put("types",types.get());
        map.put("category",category.get());
        map.put("price",price.get());

        model.addbudget(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult baseResult) {
                        if (baseResult.issucc){
                            ToastUtils.showShort(baseResult.msg);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
